/**
 * 
 */
package no.gnet.util;

import java.net.URL;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.mail.HtmlEmail;

/**
 * @author Gaute
 *
 */
public class Mailer {

	public static void sendMail(String to,String subject, String body) throws Exception{
		 Properties props = System.getProperties();

	      // -- Attaching to default Session, or we could start a new one --

	      props.put("mail.smtp.host", "smtp.gmail.com");
	      props.put("mail.smtp.starttls.enable", "true");
	      props.put("mail.smtp.user", "gautesmaaland");
	      props.put("mail.smtp.auth", "true");
	      props.put("mail.smtp.debug", "true");
			props.put("mail.smtp.socketFactory.port", "465");
			props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			props.put("mail.smtp.socketFactory.fallback", "false");	      
	      
	      SMTPAuthenticator aut = new Mailer().new SMTPAuthenticator();
	      Session session = Session.getInstance(props, aut);
	      	      
	      // -- Create a new message --
	      MimeMessage msg = new MimeMessage(session);

	      // -- Set the FROM and TO fields --
	      msg.setFrom(new InternetAddress("gautesmaaland@gmail.com"));
	      msg.setRecipients(Message.RecipientType.TO,
	        InternetAddress.parse(to, false));

	      // -- We could include CC recipients too --
	      // if (cc != null)
	      // msg.setRecipients(Message.RecipientType.CC
	      // ,InternetAddress.parse(cc, false));

	      // -- Set the subject and body text --
	      msg.setSubject(subject);
	      msg.setText(body,"UTF-8");
	      
	      
	      

	      msg.setSentDate(new Date());

	      // -- Send the message --
	      Transport.send(msg);

	      System.out.println("Message sent OK.");
	}
    private class SMTPAuthenticator extends javax.mail.Authenticator
    {
        public PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication("gautesmaaland", "mercedes1004");
        }
    }
}
